package com.smart.sso.server.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.smart.mvc.model.Pagination;
import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.sso.client.SessionUtils;
import com.smart.sso.server.common.LoginUser;
import com.smart.sso.server.controller.common.BaseController;
import com.smart.sso.server.log.SystemControllerLog;
import com.smart.sso.server.model.App;
import com.smart.sso.server.model.Group;
import com.smart.sso.server.model.User;
import com.smart.sso.server.model.UserGroup;
import com.smart.sso.server.service.GroupService;
import com.smart.sso.server.service.UserGroupService;
import com.smart.sso.server.service.UserService;
import com.smart.sso.server.util.OperationUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags="组队管理")
@Controller
@RequestMapping("/admin/group")
public class GroupController extends BaseController{
	
	@Resource
	UserService userService;
	
	@Resource
	GroupService groupService;
	
	@Resource
	UserGroupService userGroupService;
	
	@ApiOperation("初始页")
	@RequestMapping(method = RequestMethod.GET)
	public String execute() {
		return "/admin/group";
	}
	
	@ApiOperation("组列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody Result list(
			@ApiParam(value = "开始页码", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer pageNo,
			@ApiParam(value = "显示条数", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer pageSize,
			HttpServletRequest request) {
//		LoginUser loginUser = (LoginUser) request.getSession().getAttribute("CURRENT_USER");
//		Integer groupId = loginUser.getGid();
//		Pagination<Group> pagination = new Pagination<>(pageNo , pageSize);
//		groupService.findPaginationByName(null, groupId, pagination);
		Pagination<Group> pagination = new Pagination<>(pageNo, pageSize);
		groupService.findPaginationByName(null, null, pagination);
		return Result.createSuccessResult().setData(pagination);
	}
	
	@ApiOperation("组员页面")
	@RequestMapping(value="/list/page" , method = RequestMethod.GET)
	public String listMemberPage(@ApiParam(value="组id" , required = true) @ValidateParam({Validator.NOT_BLANK}) Integer id , Model model) {
		model.addAttribute("gid", id);
		Group group = groupService.get(id);
		String groupName = group.getName();
		model.addAttribute("groupname", groupName);
		return "/admin/groupList";
	}
	
	@ApiOperation("组成员列表")
	@RequestMapping(value="/list/member" , method = RequestMethod.GET)
	public @ResponseBody Result listMember(
			@ApiParam(value = "组id" , required = true) @ValidateParam({Validator.NOT_BLANK}) Integer gid,
			@ApiParam(value = "开始页码" , required = true) @ValidateParam(Validator.NOT_BLANK) Integer pageNo,
			@ApiParam(value = "显示条数"  ,required = true) @ValidateParam({Validator.NOT_BLANK}) Integer pageSize,
			HttpServletRequest request) {
		List<UserGroup> userGroups = userGroupService.findByUserGroupId(null, gid);
		List<User> users = new ArrayList<>();
		for(int i =0 ; i<userGroups.size(); i++) {
			User user = userService.get(userGroups.get(i).getUid());
			users.add(user);
		}
		Pagination<User> pagination = new Pagination<>(pageNo, pageSize);
		pagination.setList(users);
		return Result.createSuccessResult().setData(pagination);
	}
	
	@ApiOperation("新增/修改页")
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(@ApiParam(value = "id") Integer id, Model model) {
		Group group;
		if (id == null) {
			group = new Group();
		}
		else {
			group = groupService.get(id);
		}
		model.addAttribute("group", group);
		return "/admin/groupEdit";
	}
	
	@ApiOperation("新增/修改提交")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_group_add ,description = "新增组群")
	public @ResponseBody Result save(
			HttpServletRequest request,
			@ApiParam(value = "id") Integer id,
			@ApiParam(value = "名称", required = true) @ValidateParam({ Validator.NOT_BLANK }) String name,
			@ApiParam(value = "是否启用", required = true) @ValidateParam({ Validator.NOT_BLANK }) Boolean isEnable,
			@ApiParam(value = "描述", required = true) @ValidateParam({ Validator.NOT_BLANK }) String description) {
//		LoginUser loginUser = (LoginUser) request.getSession().getAttribute("CURRENT_USER");
//		Integer groupId = loginUser.getGid();
		Group group;
		if (id == null) {
			group = new Group();
			group.setPid(0);//设置父组id
		}
		else {
			group = groupService.get(id);
		}
		group.setName(name);
		group.setIsEnable(isEnable);
		group.setDescription(description);
		groupService.save(group);
		return Result.createSuccessResult();
	}
	
	@ApiOperation("删除组/提交")
	@RequestMapping(value = "/delete" , method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_group_delete , description = "删除组群")
	public @ResponseBody Result deleteGroup(
			@ApiParam(value="ids" , required = true) @ValidateParam({Validator.NOT_BLANK}) String ids) {
		List<Integer> idList = getAjaxIds(ids);
		userGroupService.deleteByGroupIds(idList);
		for(int i=0 ; i< idList.size() ; i++) {
			groupService.deleteById(idList.get(i));
		}
		return Result.createSuccessResult().setMessage("删除组成功！");
	}
	
	@ApiOperation("新增组员页面")
	@RequestMapping(value="/addmember/page" , method = RequestMethod.GET)
	public String listaddMemberPage(@ApiParam(value="组id" , required = true) @ValidateParam({Validator.NOT_BLANK}) Integer gid, Model model) {
		List<UserGroup> userGroups = userGroupService.findByUserGroupId(null, gid);
		List<Integer> members = new ArrayList<Integer>();
		for(int i =0 ; i<userGroups.size() ; i++) {
			members.add(userGroups.get(i).getUid());
		}
		model.addAttribute("members", members);
		model.addAttribute("gid" , gid);
		Group group = groupService.get(gid);
		String groupName = group.getName();
		model.addAttribute("groupname", groupName);
		return "/admin/allmemberList";
	}
	
	@ApiOperation("新增组员/列表")
	@RequestMapping(value = "/addmember/list" , method = RequestMethod.GET)
	public @ResponseBody Result addMemberList(@ApiParam(value="gid") Integer gid , 
			@ApiParam(value = "开始页码", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer pageNo,
			@ApiParam(value = "显示条数", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer pageSize,
			Model model) {
//		List<UserGroup> userGroups = userGroupService.findByUserGroupId(null, gid);
//		List<Integer> members = new ArrayList<Integer>();
//		for(int i =0 ; i<userGroups.size() ; i++) {
//			members.add(userGroups.get(i).getUid());
//		}
//		model.addAttribute("members", members);	//用于标记已存在组中的成员
		return Result.createSuccessResult().setData(userService.findPaginationByAccount(null, new Pagination<User>(pageNo, pageSize)));
	}
	
	@ApiOperation("新增组员/提交")
	@RequestMapping(value="/addmember/submit" , method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_groupmemeber_add , description="新增组员")
	public @ResponseBody Result addMemberSubmit(
			@ApiParam(value = "gid") Integer gid ,
			@ApiParam(value = "ids", required = true) @ValidateParam({ Validator.NOT_BLANK }) String ids,
			Model model) {
		List<Integer> idList = getAjaxIds(ids);
		for(int i = 0 ; i < idList.size() ; i++) {
			UserGroup userGroup = new UserGroup(idList.get(i) , gid);
			userGroupService.save(userGroup);
		}
		return Result.createSuccessResult().setMessage("添加组员成功！");
	}
	
	@ApiOperation("删除组员/提交")
	@RequestMapping(value = "/delete/submit" , method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_groupmember_delete , description = "删除组员")
	public @ResponseBody Result deleteMemberSubmit(
			@ApiParam(value="gid") Integer gid,
			@ApiParam(value="ids" , required = true) @ValidateParam({Validator.NOT_BLANK}) String ids) {
		List<Integer> idList = getAjaxIds(ids);
		for(int i=0 ; i< idList.size() ; i++) {
			UserGroup userGroup = userGroupService.findByUserGroupId(idList.get(i) , gid).get(0);
			userGroupService.delete(userGroup);
		}
		return Result.createSuccessResult().setMessage("删除组员成功！");
	}
}
